package com.mfangsoft.zhuangjialong.integration.biotopeInfo.service.impl;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.FilenameUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.mfangsoft.zhuangjialong.common.context.UserContext;
import com.mfangsoft.zhuangjialong.common.model.Page;
import com.mfangsoft.zhuangjialong.common.utils.PropUtis;
import com.mfangsoft.zhuangjialong.integration.biotopeInfo.mapper.BiotopeInfoMapper;
import com.mfangsoft.zhuangjialong.integration.biotopeInfo.model.BiotopeInfo;
import com.mfangsoft.zhuangjialong.integration.biotopeInfo.service.BiotopeInfoService;
import com.mfangsoft.zhuangjialong.integration.user.model.UserEntity;

import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;



@Service
public class BiotopeInfoServiceimpl implements BiotopeInfoService {
	
	@Autowired
	BiotopeInfoMapper biotopeInfoMapper;
	
	private static final Logger LOGGER = LoggerFactory.getLogger(BiotopeInfoServiceimpl.class);

	@Override
	public String addBiotopeInfoByExcel(MultipartHttpServletRequest request) {
		
		
		MultipartFile file = request.getFile("file");
		try {
			String exname = FilenameUtils.getExtension(file.getOriginalFilename());
			if(!exname.matches("^.*(xls)$")){
				return "请上传2003版本(.xls)excel文件";
			}
			
			String path = PropUtis.getValue(PropUtis.FILE_PATH) + "/dekor/excelFile/";
			
			String fileName = path + System.currentTimeMillis() + "." + exname;
			File newFile = new File (fileName);
			File fileParent = newFile.getParentFile();  
			if(!fileParent.exists()){
				fileParent.mkdirs();
			}
			file.transferTo(newFile);
			Long filialeId =  Long.parseLong(request.getParameter("filialeId"));
			List<BiotopeInfo> list = getAllByExcel(newFile,filialeId);
			biotopeInfoMapper.addBiotopeInfoByExcel(list);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	} 
	
	

	
	/**
	 * 读取Excel
	 * @param file
	 * @return
	 * @throws ReadExcelException
	 * @throws IOException 
	 * @throws BiffException 
	 */
	public static List<BiotopeInfo> getAllByExcel(File  file,Long filialeId) throws Exception {
		List<BiotopeInfo> list = new ArrayList<BiotopeInfo>();
		Workbook rwb = Workbook.getWorkbook(file);
		Sheet rs = rwb.getSheet(0);// 或者rwb.getSheet("Sheet1")
		int clos = rs.getColumns();// 得到所有的列
		int rows = rs.getRows();// 得到所有的行
		for (int i = 1; i < rows; i++) {// 行
			int j = 0;
			if("".equals(rs.getCell(0, i).getContents())){
				break;
			}
			for (; j < clos; j++) {// 列
				BiotopeInfo biotopeInfo  = new BiotopeInfo();
				 //第一个是列数，第二个是行数
				String city = rs.getCell(j++, i).getContents();// 默认最左边编号也算一列  所以这里得j++
				biotopeInfo.setCity(city);
				String area = rs.getCell(j++, i).getContents();
				biotopeInfo.setArea(area);
				String businessArea = rs.getCell(j++, i).getContents();
				biotopeInfo.setBusinessArea(businessArea);
				String houseName = rs.getCell(j++, i).getContents();
				biotopeInfo.setHouseName(houseName);
				String houseType = rs.getCell(j++, i).getContents();
				biotopeInfo.setHouseType(houseType);
				String address = rs.getCell(j++, i).getContents();
				biotopeInfo.setAddress(address);
				String getRoomTime = rs.getCell(j++, i).getContents();
				biotopeInfo.setGetRoomTime(getRoomTime);
				biotopeInfo.setFilialeId(filialeId);
				list.add(biotopeInfo);
			}
		}
		return list;

	}




	@Override
	public Page<Map<String, Object>> selectBiotopeInfoPage(Page<Map<String, Object>> page) {

		UserEntity user = UserContext.getCurrentUser();
		if(user!=null){
			Map<String,Object> map=(Map<String, Object>) page.getParam();
			if(map!=null){
				Integer level = user.getUser_level();
				if(level == 2){
					map.put("filialeId",user.getId());
					page.setParam(map);
				}
				if(level == 1){
					map.put("shopId",user.getId());
					page.setParam(map);
				}
			}
		}
		List<Map<String,Object>> biotopeInfoPage = biotopeInfoMapper.selectBiotopeInfoPage(page);
		page.setData(biotopeInfoPage);
		return page;
	}




	@Override
	public Integer updateBiotopeInfo(BiotopeInfo biotopeInfo) {
		if(biotopeInfo.getShopId()!=null && biotopeInfo.getShopId() == 0){
			biotopeInfo.setShopId(null);
		}
		if(biotopeInfo.getSellerId()!=null && biotopeInfo.getSellerId() == 0){
			biotopeInfo.setSellerId(null);
		}
		
		return biotopeInfoMapper.updateBiotopeInfo(biotopeInfo);
	}




	@Override
	public Map<String, Object> selectBiotopeInfoById(Integer id) {
		
		return biotopeInfoMapper.selectBiotopeInfoById(id);
	}




	@Override
	public List<Map<String, Object>> getShopByfilialeId(Integer id) {
		return biotopeInfoMapper.getShopByfilialeId(id);
	}




	@Override
	public List<Map<String, Object>> getSellerByShopId(Integer id) {
		return biotopeInfoMapper.getSellerByShopId(id);
	}




	@Override
	public Integer deleteBiotopeInfoById(Integer id) {
		return biotopeInfoMapper.deleteBiotopeInfoById(id);
	}
	



}
